package com.bjpowernode.springboot.controller;

import com.bjpowernode.springboot.entity.OfficeDocumentBean;
import com.bjpowernode.springboot.entity.PageBean;
import com.bjpowernode.springboot.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 导航Controller
 */
@Controller
@RequestMapping(value = "/index")
public class IndexController {

    @Autowired
    private DocumentService documentService;

    @RequestMapping("/doc-nav-page")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        PageBean pageBean = new PageBean(1);
        // 首页显示的数据
        List<OfficeDocumentBean> officeDocumentBeans = documentService.queryDocumentByPageBean(pageBean);
        int docCount = documentService.countDocuments(); // 总数
        mv.addObject("documentsList",officeDocumentBeans);
        mv.addObject("docCount",docCount);
        mv.setViewName("doc-nav-page");
        // 每次启动首页，我们都需要将向load一遍到redis中
        documentService.loadToRedis(); // 否则当前redis中的是有可能是空的，拿不到缓存数据，无法显示排行
        return mv;
    }

    @RequestMapping("/to-add-doc-page")
    public String index1() {
        return "doc-add";
    }

    @RequestMapping("/to-hot-doc-page")
    public String hot_page() {
        return "doc-hot";
    }
}
